package com.xyd.Filter;


import com.xyd.Util.SqlSessionFactoryUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("/*")
public class AllFilter implements Filter {


    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding("UTF-8");
        servletResponse.setCharacterEncoding("UTF-8");
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String path = httpServletRequest.getServletPath();
        System.out.println("["+path+"]\t:全过滤器配置请求字符类型");
        SqlSessionFactoryUtil.open();
        try{
            filterChain.doFilter(servletRequest, servletResponse);
            SqlSessionFactoryUtil.commit();
        }catch (Exception e){
            e.printStackTrace();
            SqlSessionFactoryUtil.rollback();
        }finally {
            SqlSessionFactoryUtil.close();
        }
    }

    @Override
    public void destroy() {

    }
}


